<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

/**
 * 地址控制器类
 * @package App\Http\Controllers\Admin
 */
class AddressController extends Controller
{
    /**
     * 显示地址主页
     * @param Request $request
     * @return 返回地址页面模板
     */
    public function index(Request $request) {

        $num = $request->input('num', 10);

        $data = DB::table('addresses')
            ->where(function ($query) use($request){
                $query->where('phone', 'like', '%'.$request->input('keyword').'%')->where('created_at', '!=', '');
            })
            ->paginate($num);

        return view(('admin.address.index'), ['data' => $data, 'request' => $request->all()]);
    }

    /**
     * 显示地址添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add() {
        return view('admin.address.add');
    }

    /**
     * 删除地址操作
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id) {

        $updated_at = date("Y-m-d H:i:s", time());
        $res = DB::table('addresses')->where('id', '=', $id)->update(['created_at' => '', 'updated_at' => $updated_at]);

        if ($res) {
            return redirect('admin/address/index')->with(['info' => '删除成功']);
        } else {
            return redirect('admin/address/index')->with(['info' => '删除失败']);
        }
    }

}
